package com.hbzd.protection.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.sword.wechat4j.user.TemData;
import org.sword.wechat4j.user.UserManager;

import com.hbzd.protection.entity.Giscarjb;
import com.hbzd.protection.entity.User;
import com.hbzd.protection.service.GiscarjbServiceImpl;
import com.hbzd.protection.service.UserServiceImpl;
import com.hbzd.protection.util.JavaSmsApi;


@Controller
@RequestMapping("/user")
public class UserController {
	
	@Resource
	private UserServiceImpl userServiceImpl; 
	@Resource
	private GiscarjbServiceImpl giscarjbServiceImpl; 
	
	/**
	 * 用户点击账号绑定时，首先判断该用户是否已绑定。如若绑定，则跳转到 提示界面，
	 * 获得openid，并跳转到登陆界面
	 * @param code
	 * @param request
	 * @param response
	 * @param modalMap
	 * @return
	 */
	@RequestMapping("/auth")
	public String authenticate(String code,String state,HttpServletRequest request, HttpServletResponse response, ModelMap modalMap) {
		UserManager userManager = new UserManager();
		TemData temData = userManager.getOpenId(code);
		User user = userServiceImpl.getByOpenId(temData.getOpenid());
		if(user != null){
			//如果是北斗会员,判断该用户手机号是否发生变动
			if(user.getFlag() == 1){
				List<Giscarjb> giscarjbs = giscarjbServiceImpl.listCars(user.getPhone());
				if(giscarjbs == null || giscarjbs.size()<1){
					modalMap.put("message", "nocar");
					modalMap.put("openid", temData.getOpenid());
					return "mycar-no";
				}
			}
			if("mycar".equals(state)){
				//已绑定,跳转到车辆管理
				return "redirect:/giscarjb/listCars?OwnerSimNum="+user.getPhone()+"&openid="+temData.getOpenid();
			}else if("myjifen".equals(state)){
				//跳转到积分界面
				modalMap.put("openid", temData.getOpenid());
				Integer num = userServiceImpl.getNum(temData.getOpenid());
				modalMap.put("num", num);
				return "/myjifen";
			}else{
				return "";
			}
		}else{
			//跳转到登陆界面
			modalMap.put("openid", temData.getOpenid());
			return "/login";
		}
	}
	/**
	 * 生成随机验证码
	 * @param phone
	 * @param request
	 * @param response
	 * @param modalMap
	 */
	@RequestMapping("/getyzm")
	public void getyzm(String phone,HttpServletRequest request, HttpServletResponse response, ModelMap modalMap) {
		String info = "";
		if(userServiceImpl.get(phone) != null){
			info = "该手机用户已存在！";
		}else{
			String[] result = null;
			try {
				result = JavaSmsApi.sendMessage(phone);
				request.getSession().setAttribute("yzm", result[0]);
				request.getSession().setAttribute("phone", phone);
				info = result[1];
			} catch (IOException e1) {
				e1.printStackTrace();
				info="系统异常，请稍后再试！";
			}
		}
		try {
			response.getWriter().print(info);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 将用户openid和phone进行绑定
	 * @param phone
	 * @param password
	 * @param openid
	 * @param request
	 * @param response
	 * @param modalMap
	 * @return
	 */
	@RequestMapping("/login")
	public void login(User user,HttpServletRequest request, HttpServletResponse response, ModelMap modalMap) {
		//获取验证码
		String yzm = (String) request.getSession().getAttribute("yzm");
		String phone = (String) request.getSession().getAttribute("phone");
		//
		String info = "success";
		if(user.getYzm().equalsIgnoreCase(yzm) && user.getPhone().equals(phone)){
			userServiceImpl.update(user);
			info = "success";
		}else{
			info="信息有误！";
		}
		try {
			response.getWriter().print(info);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 获取积分
	 * @param openid
	 * @param request
	 * @param response
	 * @param modalMap
	 */
	@RequestMapping("/getNum")
	public String getNum(String openid,HttpServletRequest request, HttpServletResponse response, ModelMap modalMap) {
		Integer num = userServiceImpl.getNum(openid);
		modalMap.put("num", num);
		return "/myjifen";
	}
	@RequestMapping("/updatePurchaseTime")
	public void updatePurchaseTime(String openid) {
		userServiceImpl.updatePurchaseTime(openid);
	}
}
